package org.td.engine.entity.tower;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import org.td.engine.combat.SimpleWeapon;
import org.td.engine.combat.WeaponClass;
import org.td.engine.entity.Building;
import org.td.engine.entity.EntityClass;
import org.td.engine.entity.EntityEnvironment;

public class Tower extends Building {
    
    private Turret[] turrets;
    private TowerClass towerClass;
    
    public Tower(EntityEnvironment environment, TowerClass towerClass) {
        super(environment, towerClass);
        this.towerClass = towerClass;
        turrets = new Turret[towerClass.getTurretContainerList().size()];
    }

    public void insertTurret(int index, TurretClass turretClass) {
        TurretContainer container  = towerClass.getTurretContainerList().get(index);
        if (container != null) {
            Turret newTurret = new Turret(this, turretClass, container.relX, container.relY);
            newTurret.setWeapon(new SimpleWeapon(WeaponClass.getRandom(), newTurret));
            turrets[index] = newTurret;
        }
    }

    public int getTurretSlots() {
        return turrets.length;
    }

    public int getFreeTurretSlot() {
        for (int i = 0; i < turrets.length; i++) {
            if (turrets[i] == null) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public void update(float time) {
        for (Turret t : turrets) {
            if (t != null) {
                t.update(time);
            }
        }
    }

    @Override
    public void render(SpriteBatch batch) {
        super.render(batch);
        for (Turret t : turrets) {
            if (t != null) {
                t.render(batch);
            }
        }
    }
    
    @Override
    public EntityClass getEntityClass() {
        return towerClass;
    }
    

}
